package com.mao.spring.cloud.controller;

import com.mao.spring.cloud.entity.Employee;
import com.mao.spring.cloud.util.ResultEntity;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;


/**
 * @author: Administrator
 * @description:
 * @cteate 2021-06-15-15:59
 */
@RestController
public class EmployeeController {

    private Logger logger = LoggerFactory.getLogger(EmployeeController.class);

    @RequestMapping("/provider/get/employee/remote")
    public Employee getEmployeeRemote(){
        return new Employee(123,"dsads",1245.12345);
    }

    @RequestMapping("/provider/get/employee/list/remote")
    List<Employee> getEmpListRemote(@RequestParam("keyword") String keyword){
        List<Employee> employeeList = new ArrayList<>();
        employeeList.add(new Employee(33,"456",55.545));
        employeeList.add(new Employee(315,"45616",55.545));
        employeeList.add(new Employee(451345,"4481",55.545));
        logger.info(keyword);
        return employeeList;
    }

    // HystrixCommand注解标记出问题时的熔断备份方法
    @HystrixCommand(fallbackMethod = "getEmpBackup")
    @RequestMapping("/provider/get/employee/with/breaker")
    ResultEntity<Employee> getEmp(@RequestParam("signal") String signal){
        if ("bang".equals(signal)){
            throw new RuntimeException("人工制造异常");
        }
        if ("slow-bang".equals(signal)){
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                throw new RuntimeException("慢速异常");
            }
        }
        return ResultEntity.successWithData(new Employee(555,"dsdsyh",1452.55));
    }

    ResultEntity<Employee> getEmpBackup(@RequestParam("signal") String signal){
        return ResultEntity.failed("熔断生效，执行断路"+signal);
    }
}
